<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
class Finance Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		//$this->page_auth->secure_page('finance');
		$this->load->model(array('M_school_years','M_enrollees','M_gperiods','M_student_payment_record','M_news_and_events'));
		$this->load->library(array('mpdf'));
		$this->load->helper('misc');
		$this->_set_config_items();
		
		$this->view_data['accounting_link'] = $this->accounting_link == FALSE ? NULL : $this->accounting_link;
	}
		
	public function index()
	{
		$this->page_auth->secure_page('finance');
		$this->view_data['system_message'] = $this->_msg();
	
	}
	
	/************************************************
	Payment Report
	************************************************/
	public function payment_report()
	{
		$this->load->model('M_levels','m');
		$this->load->model('m_payment_report','msr');
		$this->view_data['levels'] = (object)$this->m->get_all_levels_array();
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['payment_reports'] = $this->msr->get_payment_report();
		$this->view_data['rdate'] = function($x){return date('M d, Y',strtotime($x));};
		
	
		if($this->input->post('add_payment_report') !== FALSE)
		{
			$this->form_validation->set_rules('start_date','Start Date','required|htmlspecialchars|trim|callback__validate_date|callback__check_date');
			$this->form_validation->set_rules('end_date','Start Date','required|htmlspecialchars|trim|callback__check_date');
			$this->form_validation->set_rules('desc','Description','htmlspecialchars|trim');
			$this->form_validation->set_rules('level','Grade Levels','required');
			
			$this->form_validation->set_message('_validate_date','Start Date Cannot Be Greater than end date');
			$this->form_validation->set_message('_check_date',' %s Has an invalid date');
			
			if($this->form_validation->run() !== FALSE)
			{
				$input['pr_date_start'] = date('Y-m-d',strtotime($this->input->post('start_date')));
				$input['pr_date_end'] = date('Y-m-d',strtotime($this->input->post('end_date')));
				$input['pr_grade_levels'] = $this->input->post('level');
				$input['pr_desc'] = $this->input->post('desc');
				
				if($this->msr->insert_payment_report($input))
				{
					$this->_msg('s','New payment report created.','finance/payment_report');
				}else{
					$this->_msg('e','Unable to create payment report.','finance/payment_report');
				}
			}else{
				$this->view_data['errors'] = validation_errors() == '' ? '' : '<div class="alert alert-error">'.validation_errors().'</div>';
			}
		}
	}
	
	public function report($id = FALSE)
	{
		if($id !== FALSE AND is_numeric($id))
		{
			$id = intval($id);
			$this->load->model('m_payment_report','msr');
			$this->view_data['report'] =$data= $this->msr->get_payment_report($id);
			$this->view_data['rdate'] = function($x){return date('M d, Y',strtotime($x));};
			$this->view_data['report_result'] = $this->msr->get_report_result($data);
		}
	}
	
	public function print_payment_record($dater)
	{
		$data = $this->__create_html();
				
		$this->__create_pdf($data);
	}
	/************************************************
	End Payment Report
	************************************************/
	
	public function reports($year='',$month='')
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['auth'] = $this->authlink->hash_make(date('mdy'));
	
		//for monthly reports
		$year = $year == '' ? date('Y') : check_year($year);
		$month = $month == '' ? date('m') : check_month($month);
		
		
		$params = $this->hs->encrypt(intval($month),intval($year));
		$auth = $this->authlink->hash_make($params);
		$link = site_url('gmr/'.$params.'/'.$auth);
		
		$prefs['template'] = '
		  {table_open}<table class="payment_calendar" border="1" cellpadding="10" cellspacing="0">{/table_open}

		   {heading_row_start}<tr>{/heading_row_start}
		   {heading_previous_cell}<th><a href="{previous_url}" class="btn"><i class="icon-step-backward"></i></a></th>{/heading_previous_cell}
		   {heading_title_cell}<th colspan="{colspan}"><a href="'.$link.'" class="confirm btn btn-mini" target="_blank" title="Continue To print report?">Download Report of the Month</a> {heading}</th>{/heading_title_cell}
		   {heading_next_cell}<th><a href="{next_url}" class="btn"><i class="icon-step-forward"></i></a></th>{/heading_next_cell}
		   {heading_row_end}</tr>{/heading_row_end}

		   {week_row_start}<tr>{/week_row_start}
		   {week_day_cell}<td class="center"><span class="label">{week_day}</span></td>{/week_day_cell}
		   {week_row_end}</tr>{/week_row_end}

		   {cal_row_start}<tr>{/cal_row_start}
		   {cal_cell_start}<td class="day-format">{/cal_cell_start}

		   {cal_cell_content}{day}<br>{content}{/cal_cell_content}
		   {cal_cell_content_today}<div class="highlight">{day} <br> {content}</div>{/cal_cell_content_today}

		   {cal_cell_no_content}{day}{/cal_cell_no_content}
		   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}

		   {cal_cell_blank}&nbsp;{/cal_cell_blank}

		   {cal_cell_end}</td>{/cal_cell_end}
		   {cal_row_end}</tr>{/cal_row_end}

		   {table_close}</table>{/table_close}
		';

		$this->load->model('M_finance');
		
		$prefs['start_day']    = 'sunday';
		$prefs['month_type']   ='long';
		$prefs['day_type']     ='long';
		$prefs['show_next_prev']  = TRUE;
		$prefs['next_prev_url'] = base_url().'finance/reports';
		
		$this->load->library('calendar',$prefs);
		$view = $this->M_finance->daily_month_reports($month,$year);

		$this->view_data['calendar'] = $this->calendar->generate($year,$month,$view);	
		//For yearly reports
		$this->load->model('M_school_years','msy');
		$this->view_data['sy'] = $this->msy->get_all_school_year();
	}
	
	public function student_charges()
	{
		

	}

	/************************************************
	PDF
	************************************************/
	private function __create_pdf($html)
	{
		$filename = str_replace(' ','_','_');
		$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0);
		$mpdf->SetDisplayMode('fullpage');
		$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
		$mpdf->WriteHTML($html);
		$mpdf->Output($filename,'I');
	}
	
	
	private function __create_html()
	{
	$link = "base_url('assets/images/school_logo.png')";
	$css =
<<<css
div#header {padding:10px 0px;}
div#header p{text-align:center; font-size:14px;}
table {width:100%;border-spacing:0px;padding:10px;}
table tr.total td.priority{border:1px solid #000;}
table tr td{padding:4px 5px;}
div.profile {margin:0 15px}
css;

		$html  = '<html><head><title></title><style>'.$css.'</style></head><body>';
		$html .= '<div id="header">
					<p>Payment Record</p>
				  </div>';
		$html .= '<div class="profile">
				<p>NAME OF STUDENT: </p>
				</div>';
		
		$html .= '<table><thead><tr><th>Description</th><th>Ammount</th></tr></thead>';
		$html .= '<tbody></tbody></table>';
		
		$html .= '</body></html>';
		
		return $html;
	}
	
	/***********End For PDF***********/
	
	/************************************************
	CALLBACKS
	************************************************/
	
	public function _validate_date($s_date)
	{
		$e_date = strtotime($this->input->post('end_date'));
		$sdate = strtotime($s_date);
		if($sdate <= $e_date)
		{
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	public function _check_date($x)
	{
		$year = explode('/',$x);
		return checkdate($year['0'],$year['1'],$year['2']) ? TRUE : FALSE;
	}
}